// components/common/withdraw/index.js
import componentBehavior from '../../../behaviors/component';
import { COMPONENT_OPTIONS } from '../../../constants/common';
import { tipToast } from '../../../common/env';
import { formatPrice } from '../../../common/utils';

Component({
    behaviors: [componentBehavior],
    options: {
        ...COMPONENT_OPTIONS,
    },
    properties: {
        // 可用金额
        availableAmount: {
            type: Number,
            value: 0,
        },
        // 最小提现金额
        minAmount: {
            type: Number,
            value: 0.01 * 100,
        },
        // 最大提现金额
        maxAmount: {
            type: Number,
            value: 50000 * 100,
        },
        buttonText: {
            type: String,
            value: '确认提现',
        },
    },
    data: {
        amount: '',
        amountShow: '',
    },
    methods: {
        handleChangeAmount(e) {
            const amount = formatPrice(e.detail);
            const amountShow = formatPrice(amount, true);
            if (isNaN(amount)) {
                tipToast('请输入有效的数字');
                return;
            }
            this.setData({
                amount,
                amountShow,
            });
        },
        handleWithdrawAll() {
            const { availableAmount } = this.data;
            if (!availableAmount) {
                tipToast('可提现金额为0');
                return;
            }
            this.setData({
                amount: availableAmount,
                amountShow: formatPrice(availableAmount, true),
            });
        },
        handleWithdrawAction() {
            const { amount, availableAmount, minAmount, maxAmount } = this.data;
            if (!availableAmount) {
                tipToast('可提现金额为0');
                return;
            }
            if (amount < minAmount) {
                tipToast(`最小提现金额为${formatPrice(minAmount, true)}元`);
                return;
            }
            if (amount > availableAmount || amount > maxAmount) {
                tipToast(`超过当前最多可提现金额${formatPrice(availableAmount > maxAmount ? maxAmount : availableAmount, true)}元`);
                return;
            }
            this.triggerEvent('apply', amount);
        },
    }
})

